package com.ming.note.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.airbnb.lottie.LottieAnimationView;
import com.ming.note.R;
import com.ming.note.util.ViewUtils;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2019/04/18
 * desc   : 状态布局（网络错误，异常错误，空数据）
 */
public final class HintLayout extends FrameLayout {

    /**
     * 提示布局
     */
    private ViewGroup mMainLayout;
    /**
     * 提示图标
     */
    private LottieAnimationView animHintIcon;
    private ImageView ivHintIcon;
    /**
     * 提示文本
     */
    private TextView mTextView;
    /**
     * 按钮
     */
    private Button mBtnRefresh;

    private TextView topTxtBanner;
    private boolean isShowTopBanner;


    public HintLayout(@NonNull Context context) {
        this(context, null);
    }

    public HintLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HintLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setClickable(true);
        setFocusable(true);
        setFocusableInTouchMode(true);

    }

    /**
     * 显示
     */
    public void show() {
        //如果是第一次 初始化布局
        if (mMainLayout == null) {
            //初始化布局
            initLayout();
        }

        //初始化头部 文字提醒
        if (topTxtBanner == null) {
            initErrorTxtBanner();
        }

        //如果没有显示布局 显示布局
        if (!isShow()) {
            // 显示布局
            mMainLayout.setVisibility(VISIBLE);
        }
    }

    private void initErrorTxtBanner() {
        topTxtBanner = new TextView(getContext());
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layoutParams.height = 100;
        topTxtBanner.setBackgroundColor(Color.parseColor("#FCDCDC"));
        topTxtBanner.setGravity(Gravity.CENTER);
        topTxtBanner.setText("加载内容失败");
        topTxtBanner.setTextColor(Color.parseColor("#FF4646"));
        topTxtBanner.setVisibility(GONE);
        addView(topTxtBanner, layoutParams);
    }

    /**
     * 展示头部提醒
     */
    public void showErrorTxtBanner() {
        if (topTxtBanner == null) {
            return;
        }
        if (isShowTopBanner){
            return;
        }
        topTxtBanner.setVisibility(VISIBLE);
        isShowTopBanner = true;
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.hintlayout_error_txt_banner_top_in);
        topTxtBanner.setAnimation(animation);
        animation.start();
        topTxtBanner.postDelayed(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        }, 2000);
    }

    /**
     * 头部文字提醒隐藏动画
     */
    private void dismiss() {
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.hintlayout_error_txt_banner_out);
        topTxtBanner.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                topTxtBanner.setVisibility(GONE);
                isShowTopBanner = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 隐藏
     */
    public void hide() {

        if (mMainLayout != null && isShow()) {
            //隐藏布局
            mMainLayout.setVisibility(INVISIBLE);
        }
    }

    /**
     * 是否显示了
     */
    public boolean isShow() {
        return mMainLayout != null && mMainLayout.getVisibility() == VISIBLE;
    }

    /**
     * 设置提示图标，请在show方法之后调用
     */
    public void setIcon(@DrawableRes int id) {
        setIcon(ContextCompat.getDrawable(getContext(), id));
    }

    public void setIcon(Drawable drawable) {
        if (ivHintIcon != null) {
            ivHintIcon.setVisibility(VISIBLE);
            ivHintIcon.setImageDrawable(drawable);
        }

        if (animHintIcon != null) {
            animHintIcon.setVisibility(GONE);
        }

    }

    /**
     * 设置提示动画
     */
    public void setAnim(@RawRes int id) {
        if (animHintIcon != null) {
            animHintIcon.setVisibility(VISIBLE);
            animHintIcon.setAnimation(id);
        }

        if (ivHintIcon != null) {
            ivHintIcon.setVisibility(GONE);
        }

    }

    /**
     * 设置提示文本，请在show方法之后调用
     */
    public void setHint(@StringRes int id) {
        setHint(getResources().getString(id));
    }

    public void setHint(CharSequence text) {
        if (mTextView != null && text != null) {
            mTextView.setText(text);
        }
    }

    /**
     * 设置按钮文字
     *
     * @param id
     */
    public void setBtnText(@StringRes int id) {
        setBtnText(getResources().getString(id));
    }

    /**
     * 设置按钮文字
     *
     * @param text
     */
    public void setBtnText(CharSequence text) {
        if (TextUtils.isEmpty(text)) {
            ViewUtils.setVisibility(mBtnRefresh, GONE);
        } else {
            ViewUtils.setVisibility(mBtnRefresh, VISIBLE);
            ViewUtils.setText(mBtnRefresh, text);
        }
    }

    /**
     * 初始化提示的布局
     */
    private void initLayout() {

        mMainLayout = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.widget_hint_layout, this, false);

        animHintIcon = mMainLayout.findViewById(R.id.animHintIcon);
        ivHintIcon = mMainLayout.findViewById(R.id.ivHintIcon);
        mTextView = mMainLayout.findViewById(R.id.ivHintText);
        mBtnRefresh = mMainLayout.findViewById(R.id.btnRefresh);

        if (mMainLayout.getBackground() == null) {
            // 默认使用 windowBackground 作为背景
            TypedArray ta = getContext().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
            mMainLayout.setBackground(ta.getDrawable(0));
            ta.recycle();
        }

        addView(mMainLayout);
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        if (isShow()) {
            mBtnRefresh.setOnClickListener(l);
        } else {
            super.setOnClickListener(l);
        }
    }
}